package com.scwatch.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.scwatch.common.util.Dates;
import com.scwatch.core.domain.PushMsg;
import com.scwatch.core.domain.Site;
import com.scwatch.core.domain.SiteJpush;
import com.scwatch.core.domain.User;
import com.scwatch.core.service.JpushService;
import com.scwatch.core.service.PushMsgService;
import com.scwatch.jpush.JpushClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class JpushServiceImpl implements JpushService {

    @Autowired
    private PushMsgService pushMsgService;

    @Override
    public boolean JpushIosOrAndroid(Site site, User user, JSONObject json) {
        SiteJpush jpush = site.getSiteJpush();
        String status = jpush.getStatus();
        String isJpush = jpush.getIsJpush();
        Boolean isPush = StringUtils.isNotBlank(isJpush)? ("1".equals(isJpush)) :false;
        JpushClientUtil jpushClient = getJpushClient(site);
        String siteName="直播盐都";
        if("直播泸州".equals(site.getName())){
            siteName="直播泸州";
        }

        try {
            if (status.equals("1")) {//正式
            /*------------安卓 和 苹果  推送 --------------*/
                // pushType  0正常、1推送直播间动态、2聊天室

                String title = json.getString("title");//通知内容标题
                String msgTile = json.getString("msgTile");//消息内容标题
                String content = json.getString("content");//消息内容
                String groupTag = json.getString("groupTag");//分组信息
                Boolean isOpen = json.getBoolean("isOpen");//静默
                JSONObject extrasparam = json.getJSONObject("extrasparam");//扩展字段

                String sendValue = (title.length() > 100) ? title.substring(0, 100) + "....." : title;
                String msgid1 = null;
                String msgid2 = null;
                //设置为:isOpen true 为静默
                try {
                    msgid1 = jpushClient.sendToAllAndroid(groupTag == null ? "" : groupTag, isOpen ? "" : sendValue, isOpen ? "" : siteName, content, extrasparam == null ? "" : extrasparam.toJSONString(), isOpen, isPush);
                }finally {
                    try {
                        msgid2 = jpushClient.sendToAllIos(groupTag == null ? "" : groupTag, isOpen ? "" : sendValue, isOpen ? "" : siteName, content, extrasparam == null ? "" : extrasparam.toJSONString(), isOpen, isPush);
                    }catch (Exception e){ }
                }
                try {
                    if (!isOpen) {
                        this.SetPushMsg(msgid1, msgTile, title, "0", user, site);
                        this.SetPushMsg(msgid2, msgTile, title, "1", user, site);
                    }
                } catch (Exception e) { }
            } else {//测试
                json.put("deviceId", jpush.getDeviceId());
                getJpushTest(jpushClient, json, user, site);
            }
            return true;
        } catch (Exception e) {
            e.getStackTrace();
            return false;
        }
    }

    @Override
    public boolean JpushIos(Site site, User user, JSONObject json) {
        SiteJpush jpush = site.getSiteJpush();
        String status = jpush.getStatus();
        String isJpush = jpush.getIsJpush();
        Boolean isPush = StringUtils.isNotBlank(isJpush)? ("1".equals(isJpush)) :false;
        JpushClientUtil jpushClient = getJpushClient(site);
        try {
            if (status.equals("1")) {//正式
            /*------------安卓 和 苹果  推送 --------------*/
                String title = json.getString("title");//通知内容标题
                String msgTile = json.getString("msgTile");//消息内容标题
                String content = json.getString("content");//消息内容
                String groupTag = json.getString("groupTag");//分组信息
                JSONObject extrasparam = json.getJSONObject("extrasparam");//扩展字段
                Boolean isOpen = json.getBoolean("isOpen"); //设置为:isOpen true 为静默

                String sendValue = (title.length() > 20) ? title.substring(0, 30) + "....." : title;
                String msgid2 = jpushClient.sendToAllIos(groupTag == null ? "" : groupTag, isOpen ? "" : sendValue, isOpen ? "" : title, content, extrasparam == null ? "" : extrasparam.toJSONString(), isOpen, isPush);
                this.SetPushMsg(msgid2, msgTile, title, "1", user, site);
            } else {//测试
                json.put("deviceId", jpush.getDeviceId());
                getJpushTest(jpushClient, json, user, site);
            }
            return true;
        } catch (Exception e) {
            e.getStackTrace();
            return false;
        }
    }

    @Override
    public boolean JpushAndroid(Site site, User user, JSONObject json) {
        SiteJpush jpush = site.getSiteJpush();
        String status = jpush.getStatus();
        String isJpush = jpush.getIsJpush();
        Boolean isPush = StringUtils.isNotBlank(isJpush)? ("1".equals(isJpush)) :false;
        JpushClientUtil jpushClient = getJpushClient(site);
        String siteName="直播盐都";
        if("直播泸州".equals(site.getName())){
            siteName="直播泸州";
        }
        try {
            if (status.equals("1")) {//正式
            /*------------安卓 和 苹果  推送 --------------*/
                String title = json.getString("title");//通知内容标题
                String msgTile = json.getString("msgTile");//消息内容标题
                String content = json.getString("content");//消息内容
                String groupTag = json.getString("groupTag");//分组
                Boolean isOpen = json.getBoolean("isOpen");//是否静默
                JSONObject extrasparam = json.getJSONObject("extrasparam");//扩展字段

                String sendValue = (title.length() > 20) ? title.substring(0, 30) + "....." : title;
                String msgid1 = jpushClient.sendToAllAndroid(groupTag == null ? "" : groupTag, isOpen ? "" : sendValue, isOpen ? "" : siteName, content, extrasparam == null ? "" : extrasparam.toJSONString(), isOpen, isPush);
                this.SetPushMsg(msgid1, msgTile, title, "0", user, site);
            } else {//测试
                json.put("deviceId", jpush.getDeviceId());
                getJpushTest(jpushClient, json, user, site);
            }
            return true;
        } catch (Exception e) {
            e.getStackTrace();
            return false;
        }
    }


    //保存记录
    private boolean SetPushMsg(String msgid, String msgTile, String title, String pushState, User user, Site site) {
        PushMsg record = new PushMsg();
        record.setMessageId(msgid);
        record.setPushState(pushState);
        record.setTitle(title);
        record.setPushTitle(msgTile);
        record.setUser(user);
        record.setSite(site);
        record.setPushTime(Dates.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
        if (msgid.equals("")) {
            record.setStatus(0);
        } else {
            record.setStatus(1);
        }
        try {
            pushMsgService.save(record);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    //测试封装
    private int getJpushTest(JpushClientUtil jpushClientUtil, JSONObject json, User user, Site site) {
        int count = 0;
        try {
            String title = json.getString("title");//通知内容标题
            String msgTile = json.getString("msgTile");//消息内容标题
            String deviceId = json.getString("deviceId");//设备标识
            String content = json.getString("content");//消息内容
            JSONObject extrasparam = json.getJSONObject("extrasparam");//扩展字段
            Boolean isOpen = json.getBoolean("isOpen"); //设置为:isOpen true 为静默

            count = jpushClientUtil.sendToRegistrationId(deviceId.split(","), isOpen ? "" : title, isOpen ? "" : title, content, extrasparam == null ? "" : extrasparam.toJSONString(), isOpen);
            this.SetPushMsg("Test32", msgTile, title, "0", user, site);

            System.out.println(" 指定设备 推送信息 状态:[" + count + "] >>>>>>>>>: " + json.toJSONString());
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            return count;
        }
    }


    //推送封装
    private JpushClientUtil getJpushClient(Site site) {
        SiteJpush jpush = site.getSiteJpush();
        if (jpush != null) {
            String apiKey = jpush.getAndroidApiKey();
            String secretKey = jpush.getAndroidSecretKey();
            String iosApiKey = jpush.getIosApiKey();
            String iosSecretKey = jpush.getIosSecretKey();
            JpushClientUtil jpushClientUtil = new JpushClientUtil(apiKey, secretKey);
            return jpushClientUtil;
        }
        return null;
    }
}
